#include <iostream>
#include <stack>
using namespace std;

class Solution
{
private:
    stack<int> stack1;
    stack<int> stack2;
    bool storageFlag = false;
public:
    void push(int node) {
        if (storageFlag) {
            while (!stack2.empty()) {
                stack1.push(stack2.top());
                stack2.pop();
            }
            storageFlag = false;
        }
        stack1.push(node);
    }

    int pop() {
        if (!storageFlag) {
            while (!stack1.empty()) {
                stack2.push(stack1.top());
                stack1.pop();
            }
            storageFlag = true;
        }
        int res = stack2.top();
        stack2.pop();
        return res;
    }
};

int main() {
    Solution solution = Solution();
    solution.push(1);
    solution.push(2);
    cout << solution.pop() << endl;
    solution.push(3);
    solution.push(4);
    solution.push(5);
    cout << solution.pop() << endl;
    cout << solution.pop() << endl;
    cout << solution.pop() << endl;
    cout << solution.pop() << endl;
    return 0;
}